package org.sqp.base;

import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.TrustStrategy;

import javax.net.ssl.SSLContext;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

public class AbstractSignIn implements SignIn {
    private static AccountHelper accountHelper = new AccountHelper();

    public String get(String name) {
        String v = accountHelper.get(name);
        if (v == null) {
            throw new RuntimeException("账号密码未配置");
        }
        return v;
    }

    public String title() {
        return "";
    }

    public void login() {

    }

    public void sign() {

    }

    public HttpClient getNewClient() {
        return getNewClient(new BasicCookieStore());
    }

    public HttpClient getNewClient(CookieStore cookieStore) {
//        CookieStore cookieStore = new BasicCookieStore();
        RequestConfig requestConfig = RequestConfig.custom()
                .setCookieSpec(CookieSpecs.STANDARD)
                .build();

        CloseableHttpClient client = HttpClients.custom()
                .setDefaultRequestConfig(requestConfig)
                .setDefaultCookieStore(cookieStore)
                .build();
        return client;
    }

    public CloseableHttpClient getNewSslClient(CookieStore cookieStore) {
        SSLContext sslContext = null;
        try {
            sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
                public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
                    return true;
                }
            }).build();
        } catch (Exception e) {
            e.printStackTrace();
        }
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
        RequestConfig requestConfig = RequestConfig.custom()
                .setCookieSpec(CookieSpecs.STANDARD)
                .build();

        CloseableHttpClient client = HttpClients.custom()
                .setDefaultRequestConfig(requestConfig)
                .setDefaultCookieStore(cookieStore)
                .setSSLSocketFactory(sslsf)
                .build();
        return client;
    }

    public CloseableHttpClient getNewSslClient() {
        return getNewSslClient(new BasicCookieStore());
    }

    public String runJs(String script) {
        try {
            ScriptEngineManager engineManager = new ScriptEngineManager();
            ScriptEngine engine = engineManager.getEngineByName("JavaScript"); //得到脚本引擎
            engine.eval("function a() {" + script + "}");
            Invocable inv = (Invocable) engine;
            Object a = inv.invokeFunction("a");
            return a.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
}
